﻿/*
 * Created by SharpDevelop.
 * User: Administrator
 * Date: 2016/4/21
 * Time: 14:22
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */

using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;

namespace SharpChannel.Rpc.Serialization
{
	/// <summary>
	/// Description of SystemBinaryFormatter.
	/// </summary>
	public class DefaultBinaryFormatter : ISerializable
	{
		#region ISerializable implementation

		public byte[] Serialize(object obj)
		{
			_stream.SetLength(0);
			_stream.Position = 0;
			_formatter.Serialize(_stream, obj);
			byte[] result = new byte[_stream.Length];
			_stream.Seek(0, SeekOrigin.Begin);
			_stream.Read(result, 0, result.Length);
			return result;
		}
		public object Deserialize(byte[] buffer)
		{
			_stream.SetLength(0);
			_stream.Position = 0;
			_stream.Write(buffer, 0, buffer.Length);
			_stream.Seek(0, SeekOrigin.Begin);
			return _formatter.Deserialize(_stream);
		}

		#endregion

	    internal DefaultBinaryFormatter()
	    {
	    }



	    private readonly MemoryStream _stream = new MemoryStream(1024 * 1024);

		private readonly IFormatter _formatter = new BinaryFormatter();
		
	}
	
	
	
}
